package com.ls.sort_algorithm;

import java.util.Arrays;
import java.util.Date;

/**
 * 冒泡排序
 */
public class BubbleSort {
    public static void main(String[] args) {
        BubbleSort bubbleSort = new BubbleSort();
//        int[] arr = {3, 9, -1, -2, 10};
/*        int[] arr = {1, 2, 3, 4, 10};

        bubbleSort.bubbleSort(arr);
        System.out.println();
        System.out.println(Arrays.toString(arr));*/


        int[] arr = new int[80000];
        for (int i = 0; i < 80000; i++) {
            arr[i] = (int) (Math.random() * 8000000);
        }

        System.out.println(new Date());
        bubbleSort.bubbleSort(arr);
        System.out.println(new Date());

    }

    public void bubbleSort(int[] arr) {
        int temp = 0;
        boolean flag = false;//表示变量,表示是否进行过交换
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = true;
                }
            }
//            System.out.println(Arrays.toString(arr));
            if (!flag) {//在一趟交换中 ,一次交换都没有发生
                break;
            } else {
                flag = false;
            }
        }
    }
}
